﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LeoShi.Soft.OpenSinaAPI;
using System.IO;
using System.Xml;
using LTP.Common;

namespace TCS.Web.MembersHome
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }  
    
        protected void imgbtnLogin_Click(object sender, ImageClickEventArgs e)
        {
            if ((Session["BangShangMembers"] != null) || (Session["BangShangMembers"].ToString() != ""))
            {
                if (Session["BangShangMembers"].ToString().ToLower() != this.txtCode.Value.ToLower())
                {
                    MessageBox.Show(Page, "所填写的验证码与所给的不符 !");
                    Session["BangShangMembers"] = null;
                    return;
                }
                else
                {
                    Session["BangShangMembers"] = null;
                }
            }
            else
            {
                Response.Redirect("~/membersHome/login.aspx");
            }
            bool Authenticated = false;
            string userName = LTP.Common.PageValidate.InputText(txtName.Value.Trim(), 30);
            string Password = LTP.Common.PageValidate.InputText(txtpwd.Value.Trim(), 30);
            Authenticated = SiteLevelCustomAuthenticationMethod(userName, Password);

            if (Authenticated == true)
            {
                TCS.Model.MemberInfo member = new TCS.BLL.MemberInfo().GetModel(userName);
                TCS.Model.Weibo.WeiBo_UserInfo wb_userInfo = new TCS.BLL.Weibo.WeiBo_UserInfo().GetModelByMemberID(member.Id);
                Session["BangShangMember"] = member; //帮商会员用户信息
                Session["memberid"] = member.Id;
                if (wb_userInfo == null)
                {
                    Response.Redirect("BindWb.aspx?u=" + member.Id);
                }
                else
                {
                    checkLogin(wb_userInfo.oauth_token, wb_userInfo.oauth_token_secret, wb_userInfo.Wb_id);

                    Response.Redirect("~/membershome/default.aspx");
                   // Server.Transfer("index.aspx");
                   // Response.Write("<script>location.href='de.aspx';</script>");
                }
            }
            else
            {
                LTP.Common.MessageBox.Show(this.Page, "登录失败,请检查用户名密码。");
            }
        }
        /// <summary>
        /// 验证登陆
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPassword"></param>
        /// <returns></returns>
        private bool SiteLevelCustomAuthenticationMethod(string strUserName, string strPassword)
        {
            bool boolReturnValue = false;
            string userName = LTP.Common.PageValidate.InputText(strUserName.Trim(), 30);
            string Password = LTP.Common.PageValidate.InputText(strPassword.Trim(), 30);
            return new TCS.BLL.MemberInfo().Exists(userName, Password);
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            this.Response.Redirect("~/index.aspx");
        }

        private void checkLogin(string Token, string TokenSecret,long WB_UserID)
        {
          var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
          httpRequest.Token = Token;
          httpRequest.TokenSecret = TokenSecret;
          httpRequest.UserRemoteIP = Request.UserHostAddress;
          string basic = "http://api.t.sina.com.cn/";
          string url = "";
          url = basic + "users/show.xml?user_id=" + WB_UserID;
          string result= httpRequest.Request(url, String.Empty);

          WeiBo.SinaAPI.SinaBase.User = WeiBo.SinaAPI.UserController.GetModelByUserXml(result);
          WeiBo.SinaAPI.SinaMUsers user = WeiBo.SinaAPI.SinaBase.User;
          TCS.Model.MemberLogin memberLogin = new TCS.Model.MemberLogin();
          memberLogin.Memberid = user.id;
          memberLogin.NickName = user.name;
          memberLogin.Location = user.location;
          memberLogin.Profile_image_url = user.profile_image_url;
          memberLogin.Codename = user.screen_name;
          memberLogin.Description = user.description;
          memberLogin.Favourites_count = user.favourites_count;
          memberLogin.Followers_count = user.followers_count;
          memberLogin.Following = user.following;
          memberLogin.Friends_count = user.friends_count;
          memberLogin.Gender = user.gender;
          memberLogin.Url = user.url;
          memberLogin.WB_UserID = user.id;
          memberLogin.Statuses_count = user.statuses_count;
          Session["BangShangLogin_model"] = memberLogin;
          Session["oauth_token"] = Token;
          Session["oauth_token_secret"] = TokenSecret;
          Session["WB_USERID"] = WB_UserID;
        }
    }
}
